package com.lightheart.sphr.xiaobinggan.module.home.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.guwu.common.viewpager.PhotoViewPager;
import com.lightheart.sphr.xiaobinggan.R;
import com.lightheart.sphr.xiaobinggan.base.BaseActivity;
import com.lightheart.sphr.xiaobinggan.bean.FriendCircleEvent;
import com.lightheart.sphr.xiaobinggan.module.home.adapter.BigImageAdapter;
import com.luck.picture.lib.entity.LocalMedia;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class BigImageActivity extends BaseActivity {

    @BindView(R.id.viewPager_image)
    PhotoViewPager photoViewPager;
    @BindView(R.id.iv_back)
    ImageView mIVCancel;
    @BindView(R.id.iv_delete)
    ImageView mIVDelete;
    @BindView(R.id.tv_pic_num)
    TextView mTVPicNum;
    @BindView(R.id.ll_big_image)
    LinearLayout llBigImage;


    private BigImageAdapter imageAdapter;
    private int currentPosition;
    private List<LocalMedia> selectList = new ArrayList<>();
    private String fromType = "";
    private ArrayList<Fragment> fragments = new ArrayList<>();


    public static void launch(Activity context, int picPosition, ArrayList<LocalMedia> data, String type){
        Intent intent = new Intent(context, BigImageActivity.class);
        intent.putExtra("position", picPosition);
        intent.putExtra("type", type);
        intent.putExtra("data",data);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return  R.layout.activity_big_image;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        if(getIntent()!= null){
            currentPosition = getIntent().getIntExtra("position",0);
            selectList = getIntent().getParcelableArrayListExtra("data");
            fromType = getIntent().getStringExtra("type");
            if(TextUtils.equals("main5",fromType)){
                mIVDelete.setVisibility(View.VISIBLE);
            }else {
                mIVDelete.setVisibility(View.GONE);
            }
        }
        for (int i = 0; i < selectList.size(); i++) {
            BigImageFragment bigFragment = BigImageFragment.getInstance(currentPosition, selectList.get(i));
            fragments.add(bigFragment);
        }

        imageAdapter = new BigImageAdapter(getSupportFragmentManager(), fragments);
        photoViewPager.setAdapter(imageAdapter);
        photoViewPager.setCurrentItem(currentPosition);
        //张数
        changeTextContent();
        initListener();
    }

    private void initListener() {
        photoViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
                changeTextContent();
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }


    @OnClick({R.id.iv_back, R.id.iv_delete})
    public void onViewClick(View view){
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_delete:
                selectList.remove(currentPosition);
                imageAdapter.delPage(currentPosition);
                photoViewPager.setCurrentItem(currentPosition);
                if(selectList.size() == 0){
                    finish();
                }else{
                    changeTextContent();
                }
                EventBus.getDefault().post(new FriendCircleEvent(selectList));
                break;
        }
    }

    //改变数量
    @SuppressLint("SetTextI18n")
    private void changeTextContent(){
        mTVPicNum.setText(currentPosition+1+"/"+selectList.size());
    }


}
